# -*- coding: utf-8 -*-
from export.b2b.ugcontract import Ugcontract
from export.models import BaseProduct, BaseProduct_Sku

class ExportCatalog():
    products = []

    def __init__(self, csv, type, delimetr=';'):
        self.csv = csv
        self.delimetr = delimetr
        self.convertCSVtoListProduct()

    def convertCSVtoListProduct(self):
        csvContent = self.csv.split('\n')
        csvContent = csvContent[1:]
        ugcontract = Ugcontract()
        for csvRow in csvContent:
            if csvRow!=u'':
                csvCell = csvRow.decode('utf8').replace('"','').split(self.delimetr)
                if csvCell!=u'':
        #            try:
        #                product = BaseProduct.objects.get(sku=csvCell[0])
        #            except BaseProduct.DoesNotExist:
                    product = ugcontract.search(csvCell[0], csvCell[2])
                    if csvCell[1]:
                        product.entity_id=csvCell[1]
                        product.save()
                    self.products.append(product)